<?php

class Admin_ParameterController extends Zend_Controller_Action
{
    protected $_em;
    protected $_repo;
    
    public function init()
    {
        $this->_helper->layout()->menu= array("parameter","Country","Region","City","Guide Type","Theme","Tour Type");
    	
        $this->_em = Zend_Registry::get('doctrine');
        $this->_flash = $this->_helper->getHelper('FlashMessenger');
    }

	public function indexAction() {
	    
	}
		
	/************** REGION *************/
	public function regionAction(){
		$this->view->manager = 'MANAGEMENT REGION LIST';
	
		$id = $this->_getParam('id', 0);
		
		$this->_repo = $this->_em->getRepository('Entities\HaeBddRegion'); 
		if ( $id != 0 )
		{
			$this->_repo->deleteById($id);
		}
		$this->view->region = $this->_repo->findAll();
	}
	public function detailregionAction(){
	    $id = $this->_getParam('id', 0);
	    
	    $form = new Admin_Form_Region();
	    $this->view->form = $form;

	    if($id == 0 )
	    {
	    	$this->view->manager = 'ADD NEW REGION';
	    	if($this->getRequest()->isPost())
	    	{
	    		$this->_saveRegion($this->getRequest()->getPost());
	    		$this->_helper->flashMessenger('Add successfully');
	    	}
	    }
	    else
	    {
	    	$this->_repo = $this->_em->getRepository('Entities\HaeBddRegion');
	    	$data = $this->_repo->findOneById($id);
	    	 
	    	$this->view->manager = 'EDITOR  REGION :'.$data->getTitleEn();
	    	$form->populate($data->toArray());
	    	
	    	if($this->getRequest()->isPost())
	    	{
	    		$this->_saveRegion($this->getRequest()->getPost());
	    		$this->_helper->flashMessenger('Edit successfully');
	    	}
	    }
	}
	public function _saveRegion($postArray)
	{
	    $obj = (object) $postArray;
	    $id = $this->_getParam('id', 0);
	    $this->_repo = $this->_em->getRepository('Entities\HaeBddRegion');
	    if ($id == 0 )
	    {
	    	$Region = new Entities\HaeBddRegion;
	    }
	    else
	    {
	    	$Region = $this->_repo->findOneById($id);
	    }
	     
	    $Region->setTitleVi($obj->titleVi);
	    $Region->setTitleEn($obj->titleEn);
	    $Region->setTitleJa($obj->titleJa);
	    $Region->setTitleZh($obj->titleZh);
	    $Region->setTitleFr($obj->titleFr);
	    $Region->setCountryId($obj->countryId);
	    $Region->setStatus($obj->status);
	     
	    $this->_em->persist($Region);
	    $this->_em->flush();
	}

	/************** CITYS *************/
	public function cityAction(){
	    $this->view->manager = 'MANAGEMENT CITY LIST';
	    
	    $id = $this->_getParam('id', 0);
	    
	    $this->_repo = $this->_em->getRepository('Entities\HaeBddCity');
	    if ( $id != 0 )
	    {
	    	$this->_repo->deleteById($id);
	    }
	    $this->view->city = $this->_repo->findAll();
	}
	public function detailcityAction()
	{   
	    $id = $this->_getParam('id', 0);
	     
	    $form = new Admin_Form_City();
	    $this->view->form = $form;
	   
	   	if($id == 0 )
	    {
	    	$this->view->manager = 'ADD NEW CITY';
	    	if($this->getRequest()->isPost())
	    	{
	    		$this->_saveCity($this->getRequest()->getPost());
	    		$this->_helper->flashMessenger('Add successfully');
	    	}
	    }
	    else
	    {
	    	$this->_repo = $this->_em->getRepository('Entities\HaeBddCity');
	    	$data = $this->_repo->findOneById($id);
	    	 
	    	$this->view->manager = 'EDITOR  CITY :'.$data->getTitleEn();
	    	$form->populate($data->toArray());
	    	
	    	if($this->getRequest()->isPost())
	    	{
	    		$this->_saveCity($this->getRequest()->getPost());
	    		$this->_helper->flashMessenger('Edit successfully');
	    	}
	    }
	}
	public function _saveCity($postArray){
	    $obj = (object) $postArray;
	    $id = $this->_getParam('id', 0);
	    $this->_repo = $this->_em->getRepository('Entities\HaeBddCity');
	    if ($id == 0 )
	    {
	    	$City = new Entities\HaeBddCity;
	    }
	    else
	    {
	    	$City = $this->_repo->findOneById($id);
	    }
	    
	    $City->setTitleVi($obj->titleVi);
	    $City->setTitleEn($obj->titleEn);
	    $City->setTitleJa($obj->titleJa);
	    $City->setTitleZh($obj->titleZh);
	    $City->setTitleFr($obj->titleFr);
	    $City->setCountryId($obj->countryId);
	    $City->setRegionId($obj->regionId);
	    $City->setStatus($obj->status);
	    
	    $this->_em->persist($City);
	    $this->_em->flush();
	}
	

	
	/************** BDD FOR ALL *************/
	
	public function bddAction(){
		$id = $this->_getParam('id', 0);
		$name = $this->_getParam('name', 0);
		
		$this->view->manager = 'MANAGEMENT '. strtoupper($name) .' LIST';
		
		$respository  = 'Entities\HaeBdd'.$name;
		$this->_repo = $this->_em->getRepository($respository);
		if ( $id != 0 )
		{
			$this->_repo->deleteById($id);
		}
		$this->view->namebdd = strtolower($name);
		$this->view->object = $this->_repo->findAll();
	}
	public function detailbddAction(){
		$id = $this->_getParam('id', 0);
		$name = $this->_getParam('name', 0);
		
		$respository  = 'Entities\HaeBdd'.$name;
		$form = new Admin_Form_Bdd();
		
		$this->view->form = $form;
	
		if($id == 0 )
		{
			$this->view->manager = 'ADD NEW '.strtoupper($name);
			if($this->getRequest()->isPost())
			{
				$this->_saveBdd($this->getRequest()->getPost(),$name);
				$this->_helper->flashMessenger('Add successfully');
			}
		}
		else
		{
			$this->_repo = $this->_em->getRepository($respository);
			$data = $this->_repo->findOneById($id);
	
			$this->view->manager = 'EDITOR  '.strtoupper($name).' :'.$data->getTitleEn();
			$form->populate($data->toArray());
			if($this->getRequest()->isPost())
			{
				$this->_saveBdd($this->getRequest()->getPost(),$name);
				$this->_helper->flashMessenger('Edit successfully');
			}
		}
			
	}
	public function _saveBdd($postArray, $name){
		$obj = (object) $postArray;
		$id = $this->_getParam('id', 0);
		$respository = 'Entities\HaeBdd'.$name;
		$this->_repo = $this->_em->getRepository($respository);
		if ($id == 0 )
		{
			$Object = new $respository;
		}
		else
		{
			$Object = $this->_repo->findOneById($id);
		}
			
		$Object->setTitleVi($obj->titleVi);
		$Object->setTitleEn($obj->titleEn);
		$Object->setTitleJa($obj->titleJa);
		$Object->setTitleZh($obj->titleZh);
		$Object->setTitleFr($obj->titleFr);
		$Object->setStatus($obj->status);
			
		$this->_em->persist($Object);
		$this->_em->flush();
	}
	/************** CHANGE STATUS *************/
	public function changestatusAction()
	{
		$this->_helper->layout->disableLayout();
		$id = $this->getRequest()->getParam("id");
		$name = $this->getRequest()->getParam("name");
		
		$respository = 'Entities\Hae'.ucwords($name) ;
		$this->_repo = $this->_em->getRepository($respository);
		
		$Object = $this->_repo->findOneById($id);
		
		echo $Object->getStatus();
		if ($Object->getStatus())
		    $Object->setStatus('0');
		else
			$Object->setStatus('1');
		
		$this->_em->persist($Object);
		$this->_em->flush();
	}
	
	/************** CHANGE PRIORITY *************/
	public function changepriorityAction()
	{
		$this->_helper->layout->disableLayout();
		
		$id = $this->getRequest()->getParam("id");
		$name = $this->getRequest()->getParam("name");
		$priority = $this->getRequest()->getParam("priority");
		
		$respository = 'Entities\Hae'.ucwords($name);
		$this->_repo = $this->_em->getRepository($respository);
		
		$Object = $this->_repo->findOneById($id);
		
		$Object->setPriority($priority);
		
		$this->_em->persist($Object);
		$this->_em->flush();
	
	}
	/************** SEARCH ALL FORM FUNCTION *************/
	public function searchformAction()
	{
	    
	    $this->_helper->layout->disableLayout();
	    
	    $name = $this->getRequest()->getParam("name");
	    $field = $this->getRequest()->getParam("field");
	    $key = $this->getRequest()->getParam("key");
	    $offset = $this->getRequest()->getParam("offset",10);
	    $limitPerPage = $this->getRequest()->getParam("limitPerPage",1);
	    
	    $respository = 'Entities\Hae'.ucwords($name);
	    $this->_repo = $this->_em->getRepository($respository);
	    
	    $result = $this->_repo->searchAllForm($name,$field,$key,$offset,$limitPerPage);
	   
	    print_r($result);
	}
}